local lsp = require("lspconfig")
local shared = require("plugins.lsp") -- 确保正确引用共享配置

-- 配置 python-lsp-server (pylsp)
lsp.pylsp.setup({
  on_attach = shared.on_attach,
  capabilities = shared.capabilities,
  settings = {
    pylsp = {
      plugins = {
        pycodestyle = { enabled = false },
        pyflakes = { enabled = true },
        pylint = { enabled = false },
        jedi_completion = { enabled = true },
        jedi_hover = { enabled = true },
        jedi_references = { enabled = true },
        jedi_signature_help = { enabled = true },
        jedi_symbols = { enabled = true }
      }
    }
  }
})

-- 配置 Pyright (可选，但推荐)
lsp.pyright.setup({
  on_attach = shared.on_attach,
  capabilities = shared.capabilities,
  settings = {
    python = {
      analysis = {
        typeCheckingMode = "basic",
        autoSearchPaths = true,
        useLibraryCodeForTypes = true
      }
    }
  }
})

-- 自动检测虚拟环境
vim.api.nvim_create_autocmd("FileType", {
  pattern = "python",
  callback = function()
    local venv = vim.fn.findfile("pyproject.toml", vim.fn.getcwd() .. ";")
    if venv ~= "" then
      lsp.pyright.setup({
        root_dir = vim.fs.dirname(venv)
      })
    end
  end
})
